package com.doudoukeji.service.Activity;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Vibrator;
import android.telephony.TelephonyManager;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import org.litepal.LitePalApplication;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;


public class GlobalParams extends LitePalApplication {
    public static ArrayList<String> listPath = new ArrayList<String>();// 存放图片的路径的集合
    /**
     * 代理ip
     */
    public static String PROXY_IP = "";
    /**
     * 代理端口
     */
    public static int PROXY_PORT = 0;
    /**
     * 屏幕宽度
     */
    public static int WIN_WIDTH = 0;

    /**
     * 用户注销
     */
    public static void Logout() {
    }

    /**
     * 获取设备信息
     */
    public static Context MAIN_CONTEXT = null;
    public static String uuid;
    public static boolean isLocking = true;
    public static String unread_message_count = "";
    public static boolean success = false;
    public static int IMAGE_INDEX = 0;


    public TextView mLocationResult, logMsg;
    public TextView trigger, exit;
    public Vibrator mVibrator;

    public static String getDeviceId(Context context) {
        StringBuilder deviceId = new StringBuilder();

        // 渠道标志
        deviceId.append("client");
        try {
            // wifi mac地址
            WifiManager wifi = (WifiManager) context
                    .getSystemService(Context.WIFI_SERVICE);
            WifiInfo info = wifi.getConnectionInfo();
            String wifiMac = info.getMacAddress();
            if (!TextUtils.isEmpty(wifiMac)) {
                deviceId.append("wifi");
                deviceId.append(wifiMac);
                return deviceId.toString().replace(":", "").toLowerCase();
            }

            // IMEI（imei）
            TelephonyManager tm = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            String imei = tm.getDeviceId();
            if (!TextUtils.isEmpty(imei)) {
                deviceId.append("imei");
                deviceId.append(imei);
                return deviceId.toString();
            }
            // 序列号（sn）
            String sn = tm.getSimSerialNumber();
            if (!TextUtils.isEmpty(sn)) {
                deviceId.append("sn");
                deviceId.append(sn);
                return deviceId.toString();
            }

            // 如果上面都没有， 则生成一个id：随机码
            uuid = getUUID(context);
            if (!TextUtils.isEmpty(uuid)) {
                deviceId.append("id");
                deviceId.append(uuid);
                return deviceId.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
            deviceId.append("id").append(getUUID(context));
        }
        return deviceId.toString();

    }

    private static GlobalParams instance;

    // 单例模式中获取唯一的ExitApplication实例
    public static GlobalParams getInstance() {
        return instance;

    }

    // 遍历所有Activity并finish

    private List<Activity> activityList = new LinkedList<Activity>();

    public List<Activity> getActivityList() {
        return activityList;
    }

    // 添加Activity到容器中
    public void addActivity(Activity activity) {
        activityList.add(activity);
    }

    // 遍历所有Activity并finish

    public void exit() {

        for (Activity activity : activityList) {
            activity.finish();
        }
        // System.exit(0);

    }

    /**
     * 得到全局唯一UUID
     */
    public static String getUUID(Context context) {

        if (uuid == null || "".equals(uuid)) {
            uuid = UUID.randomUUID().toString().replaceAll("-", "");
        }
        return uuid;

        // uuid = SharedPreferencesUtils.getParam(context, "uuid", "");
        // if(TextUtils.isEmpty(uuid)){
        // uuid = UUID.randomUUID().toString().replaceAll("-", "");
        // SharedPreferencesUtils.setParam(context, "uuid", uuid);
        // }
        // return uuid;
    }

    /**
     * 检测手机号
     *
     * @return
     */
    public static String myphone(Context context) {
        TelephonyManager tm = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        String tel = tm.getLine1Number();
        return tel;
    }

    public static SpannableStringBuilder errorMsg(String estring) {
        int ecolor = Color.BLACK; // whatever color you want
        ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor);
        SpannableStringBuilder ssbuilder = new SpannableStringBuilder(estring);
        ssbuilder.setSpan(fgcspan, 0, estring.length(), 0);
        return ssbuilder;
    }

    @Override
    public void onCreate() {

        super.onCreate();
        // ImageLoaderConfiguration config = new
        // ImageLoaderConfiguration.Builder(
        // getApplicationContext())
        // .threadPriority(Thread.NORM_PRIORITY - 2)// 设置线程的优先级
        // .denyCacheImageMultipleSizesInMemory()//
        // 当同一个Uri获取不同大小的图片，缓存到内存时，只缓存一个。默认会缓存多个不同的大小的相同图片
        // .discCacheFileNameGenerator(new Md5FileNameGenerator())// 设置缓存文件的名字
        // .discCacheFileCount(60)// 缓存文件的最大个数
        // .tasksProcessingOrder(QueueProcessingType.LIFO)// 设置图片下载和显示的工作队列排序
        // .build();
        //
        // // Initialize ImageLoader with configuration
        // ImageLoader.getInstance().init(config);

        // CrashHandler crashHandler = CrashHandler.getInstance();
        // crashHandler.init(getApplicationContext());
        // 定位

        instance = this;
        // 错误日志上传，开发时关闭，上线时打开
//		 UncaughtException crashHandler = UncaughtException.getInstance();
//		 crashHandler.init();

    }

    @Override
    public void onTerminate() {
        super.onTerminate();
    }

}
